
#include "main.h"

#define   CDR_SIM_INTERNAL_MAX	15		

//CDR CDR_Temp;


unsigned int CDR_Pointer = 0;	  


/* unsigned char CDR_IMSI_ToBCD( char* s_IMSI, unsigned char* Store ) */
/* { */
/* 	unsigned char temp, i; */
/* 	i = 0; */
/* 	ClearBuffer( (unsigned char*)&CDR_Temp, sizeof(CDR) ); */

/* 	return temp; */
/* } */

//StrToBCD_Long
 
/* void cdr_test( void ) */
/* { */
	
/* }  */

 
void CDR_Init ( void )
{
	if ( Ex_LoadFrom( (unsigned char*)&CDR_Pointer, EX_ADDR_CDR_POINTER, sizeof(CDR_Pointer) )  )
	{
		if ( CDR_Pointer >= (CDR_COUNT_MAX * sizeof(CDR)) )
		{
			CDR_Pointer = EX_ADDR_START_CDR;

			if ( Ex_SaveAndCheck( EX_ADDR_CDR_POINTER, (unsigned char*)&CDR_Pointer, sizeof(CDR_Pointer) ) == FALSE )
			{
				goto E2_ERR;
			} 
		}

		return ;
	}

  E2_ERR:
	lcd_clear();	 /* EEPROM read ERROR */
	lcd_goto(0,1);
	lcd_putsf(STR__EXT__EEPROM___ERROR);
	for(;;);
	
}
 
unsigned char CDR_Save ( CDR* CallDataRecord  )
{
		
/* 	if ( Ex_LoadFrom( (unsigned char*)&CDR_Pointer, EX_ADDR_CDR_POINTER, sizeof(CDR_Pointer) )  ) */
/* 	{ */
/* 		if ( CDR_Pointer == 0xFFFF ) */
/* 			CDR_Pointer = EX_ADDR_START_CDR; */
/* 	} */
/* 	else */
/* 	{ */
/* 		return FALSE; */
/* 	} */

	if ( Ex_SaveAndCheck( CDR_Pointer, (unsigned char*)CallDataRecord, sizeof(CDR)) == FALSE )
	{
		return FALSE;
	} 

	CDR_Pointer += sizeof(CDR);
	
	if ( CDR_Pointer >= (CDR_COUNT_MAX * sizeof(CDR)) )
	{
		CDR_Pointer = EX_ADDR_START_CDR;
	} 

	if ( Ex_SaveAndCheck( EX_ADDR_CDR_POINTER, (unsigned char*)&CDR_Pointer, sizeof(CDR_Pointer) ) == FALSE )
	{
		return FALSE;
	} 

	
	return TRUE;
}
 
unsigned int CDR_AddressToIndex( unsigned int Address )
{
	unsigned int Index;

	//Index = CDR_Pointer - Address;
	Index = Address - EX_ADDR_START_CDR;
	Index /= sizeof(CDR); /* Amount of CDR (Address to Index) */
	
	return Index; 
}

unsigned int CDR_IndexToAddress ( unsigned int Index )
{
	unsigned int Address;
	
	Address = Index * sizeof(CDR);
	Address += EX_ADDR_START_CDR;

	return Address;
}
